const baseURL = window.config.apiUrl;
const http = axios.create({
  baseURL: baseURL,
  timeout: 9999999,
});

//请求拦截器
http.interceptors.request.use(
  (config) => {
    // 在发送请求之前做某事
    let token = sessionStorage.getItem("token") || "";
    config.headers["token"] = token;
    //接受JSON数据格式
    config.headers["Content-Type"] = "application/json;charset=utf-8";
    return config;
  },
  (error) => {
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);

//响应拦截器
http.interceptors.response.use(
  (response) => {
    let data = response.data;
    // 对响应数据做点什么
    if (data.code == 0) {
      return data;
    } else if (data.code == 10021 || data.code == 10020) {
      //token失效
      alert(data.msg);
      // window.location.href = baseURL + "/mt/login.html";
      return Promise.reject(data.msg);
    }
  },
  (error) => {
    // 对响应错误做点什么
    return Promise.reject(error);
  }
);
